package org.fuxia.im.listeners;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.fuxia.im.common.ClientConst;
import org.fuxia.im.common.ClientContext;
import org.fuxia.im.core.entries.ECResponse;
import org.fuxia.im.core.listeners.Listener;
import org.fuxia.im.gui.FriendManage;

import com.alibaba.fastjson.JSONArray;

public class MoveFriendListener implements Listener{

	public void trigger(Object param) {
		ECResponse resp = (ECResponse)param ;
		String newGroupKey = resp.get("newGroupKey").toString() ;
		String oldGroupKey = resp.get("oldGroupKey").toString() ;
		System.out.println(newGroupKey);
		JSONArray user = (JSONArray) resp.get("user") ;
		if(resp.getStatusCode() != null && resp.getStatusCode() == 200) {
			Map<String, List<Object>> map = ClientContext.getValue(ClientConst.FRIENDS,Map.class) ;
			List users1 = map.get(oldGroupKey) ;
			Iterator ite = users1.iterator() ;
			while(ite.hasNext()) {
				JSONArray temp = (JSONArray) ite.next() ;
				if(user.get(0).equals(temp.get(0))) {
					users1.remove(temp) ;
					break ;
				}
			}
			List users2 = map.get(newGroupKey) ;
			users2.add(user) ;
		}
		//更新table
		FriendManage frame = ClientContext.getContext().get("friendManage", FriendManage.class) ;
		frame.setTableData() ;
		
	}

}
